//
//  MyClockViewController.m
//  baodongPro
//
//  Created by feng on 16/2/28.
//  Copyright © 2016年 proper. All rights reserved.
//

#import "MyClockViewController.h"
#import "UIView+FBDQuickCreateUI.h"
#define leftRightButtonWH      80.0
#define paddingLR    40.0

@interface MyClockViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    UILabel* rightLabel;
    UILabel* middelLabel;
    NSTimer* timer;
    NSInteger fen;
    NSInteger miao;
    NSInteger h_miao;
    BOOL starting;
    UIButton* leftButton;
    UIButton* rightButton;
    NSMutableArray *everyOneArray;

    UITableView *myListTableView;


}

@end

@implementation MyClockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"秒表";
    starting=NO;
    everyOneArray=[NSMutableArray array];
    
    UIView* topView=[self.view fbd_quickCreateUIViewWithFrame:CGRectMake(0, NavTop+10,ScreenW , 200)];
    topView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:topView];
    
    CGRect rightCGRect=CGRectMake(ScreenW-240, 20, 200, 20);
    rightLabel=[self.view fbd_quickCreateUILabelWithFrame:rightCGRect];
    rightLabel.text=@"00 : 00 : 00";
    rightLabel.font=[UIFont systemFontOfSize:13];
    rightLabel.textAlignment=NSTextAlignmentRight;
    rightLabel.backgroundColor=[UIColor whiteColor];
    [topView addSubview:rightLabel];
    
    CGRect middelCGRect=CGRectMake(0, CGRectGetMaxY(rightCGRect)+20, ScreenW, 100);
    middelLabel=[self.view fbd_quickCreateUILabelWithFrame:middelCGRect];
    middelLabel.text=@"00 : 00 : 00";
    middelLabel.font=[UIFont systemFontOfSize:30];
    middelLabel.textAlignment=NSTextAlignmentCenter;
    middelLabel.backgroundColor=[UIColor whiteColor];
    [topView addSubview:middelLabel];
    
    CGRect leftButtonCGRect=CGRectMake(paddingLR, CGRectGetMaxY(topView.frame)+20, leftRightButtonWH, leftRightButtonWH);
    leftButton=[self.view fbd_quickCreateUIButtonWithFrame:leftButtonCGRect];
    leftButton.layer.cornerRadius=leftRightButtonWH/2.0;
    [leftButton setTitle:@"计次" forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];

    
    
    CGRect rightButtonCGRect=CGRectMake(ScreenW-paddingLR-leftRightButtonWH, CGRectGetMaxY(topView.frame)+20, leftRightButtonWH, leftRightButtonWH);
    rightButton=[self.view fbd_quickCreateUIButtonWithFrame:rightButtonCGRect];
        rightButton.layer.cornerRadius=leftRightButtonWH/2.0;
    [rightButton setTitle:@"开始" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(beginStarTimer:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightButton];
    
    
    CGFloat originTableY=CGRectGetMaxY(rightButtonCGRect);
    myListTableView=[[UITableView alloc] initWithFrame:CGRectMake(0, originTableY, ScreenW, ScreenH-originTableY) style:UITableViewStylePlain];
    myListTableView.backgroundColor=[UIColor grayColor];
    myListTableView.delegate=self;
    myListTableView.dataSource=self;
    [self.view addSubview:myListTableView];
    
    
    
    
}
#pragma mark ----- 左边的button 被点击了
-(void)leftButtonPressed:(UIButton*)sender
{

    if (starting==YES)
    {
        [sender setTitle:@"计次" forState:UIControlStateNormal];
        NSString* nowTimeStr=    [self getTimerStringByNow];
        [everyOneArray addObject:nowTimeStr];
        [myListTableView reloadData];
        [myListTableView scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionBottom animated:YES];
        
    }else{

        [everyOneArray removeAllObjects];
        [myListTableView reloadData];
        
        fen=miao=h_miao=0;
        
        rightLabel.text=middelLabel.text=@"00 : 00 : 00";
        starting=NO;
        

    
    
    
    }




}

//／／／／／／／／



#pragma mark ----- 右边的button 被点击了

-(void)beginStarTimer:(UIButton*)sender
{
    
    
    starting = !starting;
    if (starting)
    {
    [leftButton setTitle:@"计次" forState:UIControlStateNormal];
     timer=[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(beginJiShi) userInfo:nil repeats:YES];
    [sender setTitle:@"暂停" forState:UIControlStateNormal];
        
        
    }else{
        [leftButton setTitle:@"复位" forState:UIControlStateNormal];
        [sender setTitle:@"开始" forState:UIControlStateNormal];
        [timer invalidate];
        timer=nil;
    
    
    }

}
-(void)beginJiShi
{

    h_miao++;
    
    if (h_miao==100)
    {
        h_miao=0;
        miao++;
    }
    if (miao==60) {
        miao=0;
        fen++;
    
    }


    NSString* showTime=[self getTimerStringByNow];
    rightLabel.text=showTime;
    middelLabel.text=showTime;
    
    
}

-(NSString*)getTimerStringByNow
{
    NSString* showTime=[NSString stringWithFormat:@"%ld : %ld : %ld",fen,miao,h_miao];
    return showTime;
}


#pragma  mark ----------UITableViewDelegate &&UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return everyOneArray.count;

}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString* indentifier=@"numbersCells";
    UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
        cell.backgroundColor=[UIColor grayColor];
    }
    
    cell.textLabel.text=[everyOneArray objectAtIndex:indexPath.row];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
